package com.jt.assistant.custom_view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.jt.assistant.R;


/**
 * Created by alany on 2017/4/12.
 */
public class UpdateDialog extends Dialog {
    private Context context;
    private String title;
    private String message;
    private String confirmBtnText = "";
    private String cancelBtnText = "";
    private ClickListenerInterface clickListenerInterface;

    public interface ClickListenerInterface {
        void doConfirm();

        void doCancel();
    }

    public UpdateDialog(Context context, String title, String message) {
        super(context);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        this.context = context;
        this.message = message;
        this.title = title;
    }

    public void setButtonTxt(String confirmBtnText, String cancelBtnText) {
        this.confirmBtnText = confirmBtnText;
        this.cancelBtnText = cancelBtnText;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    public void init() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.my_update_dialog, null);
        setContentView(view);

        TextView tvTitle = (TextView) view.findViewById(R.id.title);
        TextView tvMessage = (TextView) view.findViewById(R.id.message);
        Button btnConfirm = (Button) view.findViewById(R.id.confirm);
        Button btnCancel = (Button) view.findViewById(R.id.cancel);

        tvMessage.setText(message);
        tvTitle.setText(title);
        if (!confirmBtnText.isEmpty() && !cancelBtnText.isEmpty()) {
            btnConfirm.setText(confirmBtnText);
            btnCancel.setText(cancelBtnText);
        }

        btnConfirm.setOnClickListener(new CustomDialogClickListener());
        btnCancel.setOnClickListener(new CustomDialogClickListener());

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
    }

    public void setClicklistener(ClickListenerInterface clickListenerInterface) {
        this.clickListenerInterface = clickListenerInterface;
    }

    private class CustomDialogClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
                case R.id.confirm:
                    clickListenerInterface.doConfirm();
                    break;
                case R.id.cancel:
                    clickListenerInterface.doCancel();
                    break;
            }
        }
    }


}